///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_ZEXTREAL_H_
#define _BUOLA_ZZ_ZEXTREAL_H_

#include <buola/zz/zbase.h>
#include <buola/zz/zinteger.h>

namespace buola { namespace zz {

template<typename tReal>
class ZExtReal : public ZBase<ZExtReal<tReal>>
{
public:
    template<typename tIt>
    struct tAttribute
    {
        typedef tReal tType;
    };

    ZExtReal()
    {}
    
    ZExtReal(const ZExtReal &pInt)=default;
    ZExtReal(ZExtReal&&)=default;
    
    template<typename tIt,typename tAttr,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx &pCtx,const tSk &pSk) const;
};

template<typename tReal>
template<typename tIt,typename tAttr,typename tCtx,typename tSk>
bool ZExtReal<tReal>::Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx&,const tSk &pSk) const
{
    skip_over(pB,pE,pSk);

    if(pB==pE) return false;
    tIt lI=pB;

    //check if it starts with a sign
    tReal lNum=0.0;
    tReal lDen=1.0;
    switch(*lI)
    {
    case '-':
        lDen=-1.0;
    case '+':
        ++lI;
        if(lI==pE) return false;
        break;
    default:
        break;
    }

    //check if it is nan or inf
    if(*lI=='n')
    {
        ++lI;
        if(lI==pE) return false;
        if(*lI!='a') return false;
        ++lI;
        if(lI==pE) return false;
        if(*lI!='n') return false;
        ++lI;
        pB=lI;
        pAttr=lDen<tReal(0.0)?-NAN:NAN;
        return true;
    }
    if(*lI=='i')
    {
        ++lI;
        if(lI==pE) return false;
        if(*lI!='n') return false;
        ++lI;
        if(lI==pE) return false;
        if(*lI!='f') return false;
        ++lI;
        pB=lI;
        pAttr=lDen<tReal(0.0)?-INFINITY:INFINITY;
        return true;
    }
    
    //read the first character of the integer part of the mantissa
    bool lPoint=false;
    bool lExp=false;
    switch(*lI)
    {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        lNum=*lI-'0';
        ++lI;
        break;
    case '.':
        lPoint=true;
        ++lI;
        break;
    default:
        return false;
    }
    
    //read the rest of the integer part of the mantissa
    while(lI!=pE&&!(lExp||lPoint))
    {
        switch(*lI)
        {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            lNum*=10;
            lNum+=*lI-'0';
            ++lI;
            break;
        case '.':
            lPoint=true;
            ++lI;
            break;
        case 'e':
        case 'E':
            lExp=true;
            break;
        default:
            pB=lI;
            pAttr=lDen*lNum; //lDen is just the sign, so multiply instead of dividing
            return true;
        }
    }
    
    //read the decimal part of the mantissa
    if(lPoint)
    {
        while(lI!=pE&&!lExp)
        {
            switch(*lI)
            {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                lNum*=10;
                lDen*=10;
                lNum+=*lI-'0';
                ++lI;
                break;
            case 'e':
            case 'E':
                lExp=true;
                break;
            default:
                pB=lI;
                pAttr=lNum/lDen;
                return true;
            }
        }
    }
    
    if(lExp)
    {
        tIt lIBefore=lI;
        ++lI;
        
        ZInteger<int64_t,10> lIntParser;
        int lExp;
        UUnused lCtx,lAttr;
        if(lIntParser.Parse(lI,pE,lExp,lCtx,lAttr))
        {
            pB=lI;
            using namespace std;
            pAttr=(tReal)pow(10,lExp)*lNum/lDen;
        }
        else
        {
            pB=lIBefore;
            pAttr=lNum/lDen;
        }
        return true;
    }
    else
    {
        pB=lI;
        pAttr=lNum/lDen;
        return true;
    }
}

inline ZExtReal<double> ext_dbl()
{
    return {};
}

inline ZExtReal<float> ext_flt()
{
    return {};
}

/*namespace zz*/ } /*namespace buola*/ }

#endif
